package com.open.source.kafka.delay;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ZonLen since on 2022/8/16 下午10:19
 */
@Slf4j
@ToString
@Getter
public class DelayTopicPartition implements Delayed {

  private static final DelayQueue<DelayTopicPartition> delayeds = new DelayQueue<>();


  private final Long expire;

  private final String topic;

  private final Integer partition;

  public DelayTopicPartition(String topic, Integer partition, Long delayTime) {
    this.topic = topic;
    this.partition = partition;
    this.expire = delayTime + System.currentTimeMillis();
  }

  @Override
  public long getDelay(TimeUnit unit) {
    return unit.convert(expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
  }

  @Override
  public int compareTo(Delayed o) {
   return Long.compare(getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));
  }
}

